package com.test.springCloud.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;

@RestController
public class MsgInfoController {

    @Autowired
    private RestTemplate restTemplate;

    private static final String PREFIX = "http://localhost:8072";//本项目提供的配置port,也就是服务器提供者的端口号

    @PostMapping("/client/lxf/msgList")
    public String msgList(@RequestBody JSONObject req){

        Integer sessionId = req.getInteger("sessionId");
        //设置请求头
        HttpHeaders headers = new HttpHeaders();
        MediaType type= MediaType.parseMediaType("application/json;charset=UTF-8");
        headers.setContentType(type);

        //设置请求体
        HashMap<String, Integer> map = new HashMap<String,Integer>();
        map.put("userName", sessionId);
        HttpEntity<HashMap<String,Integer>> request = new HttpEntity<HashMap<String,Integer>>(map, headers);

        //发起请求
        ResponseEntity<String> response = restTemplate.postForEntity(PREFIX+"/lxf/msgList", request, String.class);

        return response.getBody();
    }
}
